package mapeditor;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class PaintPanel extends JPanel {

    final static int GRID_X = 10;
    final static int GRID_Y = 10;
    final static int GAP = 0;
    final static int ROWS = 55;
    final static int COLUMNS = 80;
    private Field fields[][] = new Field[ROWS][COLUMNS];
    private Color color = Color.BLACK;
    private Image image;


    void save(File selectedFile) {
        PrintStream ps = null;
        try {
            ps = new PrintStream(selectedFile);
            for (Field[] row : fields) {
                for (Field field : row) {
                    ps.print(Field.getShortForColor(field.c));
                }
                ps.println();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PaintPanel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ps.close();
        }
    }
   

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
        repaint();
    }

    

    public PaintPanel() {
        for (int i = 0; i < fields.length; i++) {
            Field row[] = fields[i];
            for (int j = 0; j < row.length; j++) {
                Rectangle r = new Rectangle(j * (GRID_X+GAP), i * (GRID_Y+GAP), GRID_X, GRID_Y);
                row[j] = new Field(Color.WHITE, r);
            }
        }
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                handleMouse(e);
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                handleMouse(e);
            }
        });
        repaint();
    }

    @Override
    public void paint(Graphics gc) {
        if (image != null) {
            gc.drawImage(image, 0, 0, getWidth(), getHeight(), new ImageObserver() {
                public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                    repaint();
                    return (infoflags & ImageObserver.ALLBITS) == 0;
                }
            });
        }
        for (int i = 0; i < fields.length; i++) {
            Field row[] = fields[i];
            for (int j = 0; j < row.length; j++) {
                if (row[j].c == Color.WHITE && image != null) {
                    continue;
                }
                gc.setColor(row[j].c);
                gc.fillRect(row[j].r.x, row[j].r.y, row[j].r.width, row[j].r.height);
            }
        }
    }

    private void handleMouse(MouseEvent e) {
        Point p = e.getPoint();
        if (p.x < 0 || p.x >= (GRID_X + GAP) * COLUMNS ||
                p.y < 0 || p.y >= (GRID_Y + GAP) * ROWS) {
            return;
        }
        int x = p.x / (GRID_X + GAP);
        int y = p.y / (GRID_Y + GAP);
        Field f = fields[y][x];
        f.c = getColor();
        repaint(f.r);
    }

    public void setLoadedPanel(char[][] mapArray){
        for (int y = 0; y < 55; y++) {
            for (int x = 0; x < 80; x++) {
                Field f = fields[y][x];
                if(mapArray[y][x] == 'a'){
                    f.c = Color.BLACK;
                } 
                else if(mapArray[y][x] == 'b'){
                    f.c = Color.GREEN;
                }
                else {
                    f.c = Color.WHITE;
                }
                repaint(f.r);
            }
        }
    }
}
